
def ord_test(f)
    Integer(File::basename(f).split('.')[0])
end

def sort_test(a, b)
    ord_test(a) <=> ord_test(b)
end

COMP="./rubic"

ntests_good = ntests_bad = ntests_back = 0
npassed_bad = npassed_good = npassed_back= 0

bad = Dir[File.join('tests/front/bad/*.rb')].sort {|a,b|
    sort_test a, b
}
bad.each do |t|
    ntests_bad = ntests_bad + 1

    output = `#{COMP} < #{t} 2>&1`
    if $? != 0
        npassed_bad = npassed_bad + 1
        puts " [x] PASS: #{t} was supposed to fail and did"
    else
        puts " [ ] FAIL: #{t} was supposed to fail but did not"
    end
end
puts "Bad tests passed: #{npassed_bad} / #{ntests_bad}"

puts ""
puts "Good test:"

good = Dir[File.join('tests/front/good/*.rb')].sort {|a,b|
    sort_test a, b
}
good.each do |t|
    ntests_good = ntests_good + 1

    output = `#{COMP} < #{t} 2>&1 1>/dev/null`
    if $? == 0
        npassed_good = npassed_good + 1
        puts " [x] PASS: #{t} was supposed to pass and did"
    else
        puts " [ ] FAIL: #{t} was supposed to pass but did not"
        puts output
    end
end
puts "Good tests passed: #{npassed_good} / #{ntests_good}"

puts ""
puts "Backend test:"

back = Dir['tests/back/*.rb'].sort {|a,b|
    sort_test a, b
}
back.each do |t|
    ntests_back = ntests_back + 1

    frst = File.open(t).first
    if frst =~ /# output: (.*)/
        exp_output = $1
        exp_output = exp_output.split(',').join("\n")
    else
        exp_output = false
    end

    output = `#{COMP} < #{t} 2>&1 1>test.ll && llc --disable-cfi test.ll && gcc test.s -o test && ./test`
    output.strip!
    if $? == 0 && (!exp_output || exp_output == output)
        npassed_back = npassed_back + 1
        puts " [x] PASS: #{t} was supposed to pass and did"
    elsif exp_output != output
        puts " [ ] FAIL: #{t} expected output \"#{exp_output}\" did not match actual output \"#{output}\""
        puts output
    elsif $? != 0
        puts " [ ] FAIL: #{t} failed typecheck or other error"
        puts output
    end
end
puts "Backend tests passed: #{npassed_back} / #{ntests_back}"


tot_pass = npassed_good + npassed_bad + npassed_back
tot_tests = ntests_good + ntests_bad + ntests_back
puts ""
puts "Tests passed: #{tot_pass} / #{tot_tests}"
